<?php
/**
 * Projecte
 * 
 * @version 1.0  20120213091100
 * @author jferrert@copernic.cat  13/02/2012
 * @copyright Creative commons (by-nc-nd) Reconeixement - NoComercial - CompartirIgual 
 */

require_once '../sistema/sessions.php';
require_once '../sistema/i18n.php';
require_once '../sistema/Utils.class.php';
require_once '../models/Objecte.class.php';

$controlObjecte = new CtrlObjecte();

class CtrlObjecte {
	private $_action;
    private	$_objecte;
    private $_script = "'controls/CtrlObjecte.class.php?action=";


	public function __construct(){
		$this->_action = isset($_GET["action"]) ?  $_GET["action"] : "list";
		//echo $_GET["action"];
		$this->_objecte  = new Objecte();
		//echo " - Objecte ";

		switch($this->_action){
			case 'list'   : $this->llistar();     break;
			case 'show'   : $this->mostrar();     break;
			case 'edit'   : $this->editar();      break;
			case 'new'    : $this->nou();         break;
			case 'insert' : $this->inserir();     break;
			case 'update' : $this->actualitzar(); break;
			case 'delete' : $this->eliminar();    break;
			default       : $this->llistar();
		}		
	}

	public function llistar(){
		$taula        = $this->_objecte->listRecords();
		$show         = $this->_script."show";
		$edit         = $this->_script."edit";
		$delete       = $this->_script."delete";
		$scriptTornar = $this->_script."list','capa3'";
		$scriptAfegir = $this->_script."new','capa3'";
		include "../vistes/listObjecte.php";		
	}

	public function mostrar(){
		if(isset($_GET["id"])){
			$fila         = $this->_objecte->getRecord($_GET["id"]);
			$scriptTornar = $this->_script."list','capa3'";
			include "../vistes/showObjecte.php";	
		}	
	}

	public function editar(){
		if(isset($_GET["id"])){
			$fila           = $this->_objecte->getRecord($_GET["id"],true);
			$editarId       = false; 
			$scriptAcceptar = $this->_script."update','capa3'";
			$scriptTornar   = $this->_script."list','capa3'";
			include "../vistes/editObjecte.php";	
		}	
	}

	public function nou(){
		$fila           = $this->_objecte->newRecord();
		$editarId       = !$this->_objecte->esAuto();
		$scriptAcceptar = $this->_script."insert','capa3'";
		$scriptTornar   = $this->_script."list','capa3'";
		include "../vistes/editObjecte.php";
	}
	
	public function inserir(){
		$this->_objecte->insertRecord($_POST);
		$this->llistar();
	}	
 
	public function actualitzar(){
		$this->_objecte->updateRecord($_POST);
		$this->llistar();
	}
	
	public function eliminar(){
		$this->_objecte->deleteRecord($_GET["id"]);
		$this->llistar();		
	}
}

?>
